﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PingYou.Console
{
    public class PYCommon
    {
        /// <summary>
        /// 功能日志
        /// </summary>
        /// <param name="strMessage"></param>
        public static void WriteMsg(string strMessage)
        {
            try
            {
                StackTrace st = new StackTrace();
                string methodName = st.GetFrame(1).GetMethod().Name;
                string className = st.GetFrame(1).GetMethod().DeclaringType.ToString();

                FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"Log/Log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine("");
                m_streamWriter.WriteLine("时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss[.fff] ") + " 信息：" + strMessage);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
            catch(Exception ex)
            {
                string temp = ex.Message;
            }
        }

        /// <summary>
        /// 系统错误日志
        /// </summary>
        /// <param name="myEx"></param>
        public static void WriteErrLog(string strMessage)
        {
            //depth所请求的堆栈帧的索引，在获得所调用的方法0，获得此方法的方法1，获得调用此方法的方法的方法2。。。类推
            //MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
            try
            {
                StackTrace st = new StackTrace();
                string methodName = st.GetFrame(1).GetMethod().Name;
                string className = st.GetFrame(1).GetMethod().DeclaringType.ToString();

                FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"Log/LogErr.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine("");
                m_streamWriter.WriteLine("类名：" + className + "; 方法：" + methodName + "; 时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss[.fff] ") + " 信息：" + strMessage);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
            catch
            {

            }
        }
    }
}
